const app = getApp();
var sliderWidth = 96; // 需要设置slider的宽度，用于计算中间位置

Page({
    data: {
        tabs: ["全部", "待付款", "待发货",  "待收货", "待评价"],
        activeIndex: 0,
        sliderOffset: 0,
        expressHidden: true
    },
    onLoad: function (options) {
        var that = this;
        this.setData({
            activeIndex: options.tab,
            hasUserInfo: options.hasUserInfo||false
        });
        console.log(options.hasUserInfo || false);
        this.getOrderList(options.tab);
        console.log(options.tab);
        this.pushingGoodsData();
        wx.getSystemInfo({
            success: function (res) {
                that.setData({
                    sliderOffset: res.windowWidth / that.data.tabs.length * that.data.activeIndex,
                    windowWidth: res.windowWidth
                });
            }
        });
    },
    tabClick: function (e) {
        this.setData({
            sliderOffset: e.currentTarget.offsetLeft,
            activeIndex: e.currentTarget.id
        });
        this.getOrderList(e.currentTarget.id);
    },
    /**
     * 获取订单列表
     */
    getOrderList: function (orderType) {
        var that = this;
        var params = {
            userID: app.globalData.userID,
            orderType: orderType,//0：全部，1：待支付，2：待收货，3：待评价
        };
        app.network.requestPost(app.route.ROUTE_GET_ORDER_LIST, params, '正在获取数据', function (res) {
            that.setData({
                orderList: res.context.orderList
            })
        }, function (res) {
            wx.showToast({
                title: '加载数据失败',
            })
        }) 
    },
    pushingGoodsData: function () {
        var that = this;
        var params = {
            userID: app.globalData.userID,
            type: 0
        }
        app.network.requestPost(app.route.ROUTE_PUSHING_GOODS, params, '正在获取数据', function (res) {
            //res就是我们请求接口返回的数据
            if (res.success) {
                var _pushingList = res.context.mySpecialList;
                that.setData({
                    pushingList: _pushingList
                });
            } else {
                wx.showToast({
                    title: res.msg,
                })
            }
        }, function (res) {
            wx.showToast({
                title: '加载数据失败',
            })
        })
    },
    toGoodsTypePage: function (e) {
        var _goodsId = e.currentTarget.dataset.goodsid;
        wx.navigateTo({
            url: '../goodsDetail/goodsDetail?goodsId=' + _goodsId,
        })
    },
    cancelOrderTap: function(e) {
        var that = this;
        var _orderID = e.currentTarget.dataset.orderid;
        var params = {
            userID: app.globalData.userID,
            orderID: _orderID
        }
        app.network.requestPost(app.route.ROUTE_CANCEL_ORDER, params, '正在取消订单', function (res) {
            //res就是我们请求接口返回的数据
            if (res.success) {
                for (var index in that.data.orderList) {
                    if (that.data.orderList[index].orderID == _orderID) {
                        that.data.orderList.splice(index, 1);
                    }
                }
                that.setData({
                    orderList: that.data.orderList
                });
                wx.showToast({
                    title: res.msg,
                })
            } else {
                wx.showToast({
                    title: res.msg,
                })
            }
        }, function (res) {
            wx.showToast({
                title: '加载数据失败',
            })
        })
    },
    delOrderTap: function(e) {
        var that = this;
        var _orderID = e.currentTarget.dataset.orderid;
        var params = {
            userID: app.globalData.userID,
            orderID: _orderID
        }
        app.network.requestPost(app.route.ROUTE_DELETE_ORDER, params, '正在删除订单', function (res) {
            //res就是我们请求接口返回的数据
            if (res.success) {
                for (var index in that.data.orderList) {
                    if (that.data.orderList[index].orderID == _orderID) {
                        that.data.orderList.splice(index, 1);
                    }
                }
                that.setData({
                    orderList: that.data.orderList
                });
                wx.showToast({
                    title: res.msg,
                })
            } else {
                wx.showToast({
                    title: res.msg,
                })
            }
        }, function (res) {
            wx.showToast({
                title: '加载数据失败',
            })
        })
    },
    confirmOrderTap: function(e) {
        var that = this;
        var _orderNo = e.currentTarget.dataset.orderno;
        var params = {
            userID: app.globalData.userID,
            orderNo: _orderNo
        }
        app.network.requestPost(app.route.ROUTE_CONFIRM_ORDER, params, '正在确认收货', function (res) {
            //res就是我们请求接口返回的数据
            if (res.success) {
                var tab = 3;
                that.setData({
                    activeIndex: tab,
                    sliderOffset: that.data.windowWidth / that.data.tabs.length * tab
                });
                that.getOrderList(tab);
                wx.showToast({
                    title: res.msg,
                })
            } else {
                wx.showToast({
                    title: res.msg,
                })
            }
        }, function (res) {
            wx.showToast({
                title: '加载数据失败',
            })
        })
    },
    payWeChatTap: function(e) {
        var that = this;
        var _orderNo = e.currentTarget.dataset.orderno;
        var params = {
            userID: app.globalData.userID,
            orderNo: _orderNo
        }
        app.network.requestPost(app.route.ROUTE_WECHAT_PAY, params, '正在支付', function (res) {
            if (res.success) {
                var _package = 'prepay_id=' + res.obj.package;
                //调起微信支付  
                wx.requestPayment({
                    //相关支付参数  
                    timeStamp: res.obj.timeStamp,
                    nonceStr: res.obj.nonceStr,
                    package: _package,
                    signType: "MD5",
                    paySign: res.obj.sign,
                    //小程序微信支付成功的回调通知  
                    success: function (res) {
                        var tab = 2;
                        that.setData({
                            activeIndex: tab,
                            sliderOffset: that.data.windowWidth / that.data.tabs.length * tab
                        });
                        that.getOrderList(tab);
                    },
                    //小程序支付失败的回调通知  
                    fail: function (res) {
                    }
                })
            } else {
                wx.showToast({
                    title: res.msg,
                })
            }
        }, function (res) {
            wx.showToast({
                title: '加载数据失败',
            })
        })
    },
    evaluationTap: function(e) {
        var orderInfo = e.currentTarget.dataset.info;
        wx.navigateTo({
            url: '../evaluation/evaluation?goodsID=' + orderInfo.orderDetail[0].goodsID + '&orderNo=' + orderInfo.orderNo + '&merchantID=' + orderInfo.merchantID
        })
    },
    bindGetUserInfo: function(e) {
        var orderInfo = e.currentTarget.dataset.info;
        let code;
        var that = this;
        wx.login({
            success(res) {
                if (res.code) {
                    // 发起网络请求
                    code = res.code;
                    // 查看是否授权
                    wx.getSetting({
                        withCredentials: true,
                        success(res) {
                            if (res.authSetting['scope.userInfo']) {
                                // 已经授权，可以直接调用 getUserInfo 获取头像昵称
                                wx.getUserInfo({
                                    success(res) {
                                        var tUserInfo = res.userInfo;
                                        // 把数据传给服务器，得到openid
                                        var params = {
                                            code: code,
                                            encryptedData: res.encryptedData,
                                            iv: res.iv,
                                            type: 0
                                        }
                                        app.network.requestPost(app.route.ROUTE_LOGIN_WXUSER, params, '', function (res) {
                                            //res就是我们请求接口返回的数据
                                            if (res.success) {
                                                app.globalData.userInfo = res.context;
                                                wx.navigateTo({
                                                    url: '../evaluation/evaluation?goodsID=' + orderInfo.orderDetail[0].goodsID + '&orderNo=' + orderInfo.orderNo + '&merchantID=' + orderInfo.merchantID
                                                })
                                            } else {
                                                wx.showToast({
                                                    title: res.msg,
                                                })
                                            }
                                        }, function (res) {
                                            wx.showToast({
                                                title: '加载数据失败',
                                            })
                                            console.log(res);
                                        })

                                    }
                                })
                            }
                        }
                    })

                } else {
                    console.log('登录失败！' + res.errMsg)
                }
            }
        })
    },

    queryLogisticsTap: function (e) {
        var that = this;
        var logisticsno = e.currentTarget.dataset.logisticsno;
        if (logisticsno != null && logisticsno != '') {
            that.setData({
                expressHidden: false,
            })
            var params = {
                userID: app.globalData.userID,
                logisticsNo: logisticsno
            }
            app.network.requestPost(app.route.ROUTE_GET_LOGISTICS_INFO, params, '正在加载数据', function (res) {
                if (res.success) {
                    var deliverystatus = res.context.expressQuery.result.deliverystatus;
                    if (deliverystatus == 1) {
                        deliverystatus = '在途中';
                    } else if (deliverystatus == 2) {
                        deliverystatus = '派件中';
                    } else if (deliverystatus == 3) {
                        deliverystatus = '已签收';
                    } else if (deliverystatus == 4) {
                        deliverystatus = '派送失败(拒签等)';
                    }
                    that.setData({
                        expressData: res.context.expressQuery.result,
                        deliverystatus: deliverystatus
                    })
                } else {
                    wx.showToast({
                        title: res.msg,
                    })
                }
            }, function (res) {
                wx.showToast({
                    title: '加载数据失败',
                })
            })
      } else {
        wx.showToast({
          title: "暂无物流",
        })
      }  
    },
    hideExpress: function (e) {
        this.setData({
            expressHidden: true,
        })
    }
});